﻿'*
'*  TweetMyPC
'*  Copyright (C) 2010 TweetMyPC Team
'*
'*  This program is free software; you can redistribute it and/or modify
'*  it under the terms of the Microsoft Public License as published by
'*  Microsoft Corporation and available in 2010.
'*
'*  This program is distributed in the hope that it will be useful,
'*  but WITHOUT ANY WARRANTY; without even the implied warranty of
'*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'*  Microsoft Public License for more details.
'*
'*  You are not allowed to change/add/remove this Header, except adding
'*  to the Programmers list below or if the autor allowed you to do so!
'*
'*  You should have received a copy of the Microsoft Public License along
'*  with this program-code; if not see the license page at http://tweetmypc.codeplex.com.
'*
'* Creator of this file: BasisBit / Stefan Ludowicy (hp.stefan@gmail.com)
'*
'* COPYRIGHT:   See License in the top level directory of the sourcecode or http://tweetmypc.codeplex.com
'* PROJECT:     TweetMyPC
'* FILE:        ./Printing.vb
'* PROGRAMMERS: Stefan Ludowicy (hp.stefan@gmail.com)
'*              {add your name here is you changed the code and want to distribute it}
'*

Module Printing

    Public Function PrintThat(ByVal PrintFile As String, ByVal Printer As String, ByVal Timeout As Integer) As Integer
        Try
            If Printer.Trim.Length = 0 Then
                Printer = (New System.Drawing.Printing.PrinterSettings).PrinterName 'just use the standard printer ;-)
            End If

            Dim Proc As New System.Diagnostics.Process

            Proc.EnableRaisingEvents = True
            Proc.StartInfo.FileName = PrintFile
            Proc.StartInfo.Arguments = Chr(34) + Printer + Chr(34)
            Proc.StartInfo.Verb = "PrintTo"
            Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized 'Adobe Reader 8 and 9 still open as a visible window,
            '..., because normally you just use TweetMyPC if you are away from the Computer - after the timeout, tweetmypc will try to kill the pdf-reader
            Proc.StartInfo.CreateNoWindow = True
            Proc.Start()

            Do While Timeout > 0 AndAlso Not Proc.HasExited
                System.Threading.Thread.Sleep(700)
                Timeout -= 1
            Loop

            If Not Proc.HasExited Then
                frmTweetMyPc.DebugPrintHelper("Printing.vb::PrintPDF: pdf should be printed now after the long delay... -> try to kill the process now!")
                Proc.Kill()
            End If

            frmTweetMyPc.DebugPrintHelper("Printing.vb::PrintPDF: Closing the pdf-printer-app process now..")
            Proc.Close()
            Proc.Dispose()
            frmTweetMyPc.SendTweet("Page printed!")

        Catch ex As Exception
            frmTweetMyPc.DebugPrintHelper("Printing.vb::PrintPDF: Exception happened here - message: " & ex.Message)
            frmTweetMyPc.SendTweet("Printing failed!")
        End Try
        Return 0
    End Function

    Public Function PrintPDF(ByVal PDFFileURL As String) As Integer
        Dim workingfolder As String = PDFFileURL.Split("|")(1)
        PDFFileURL = PDFFileURL.Split("|")(0)
        '*** add your code here if you want to implement additional stuff for PDF Printing
        Dim wc As New System.Net.WebClient
        If System.IO.File.Exists(workingfolder & "\PrintThisPDF.pdf") Then
            System.IO.File.Delete(workingfolder & "\PrintThisPDF.pdf")
        End If
        wc.DownloadFile(PDFFileURL, workingfolder & "\PrintThisPDF.pdf")
        wc.Dispose()
        Return PrintThat(workingfolder & "\PrintThisPDF.pdf", "", 60)
    End Function

    Public Function PrintDOC(ByVal DOCFileURL As String) As Integer
        '*** add your code here if you want to implement additional stuff for DOC Printing
        Dim workingfolder As String = DOCFileURL.Split("|")(1)
        DOCFileURL = DOCFileURL.Split("|")(0)
        Dim wc As New System.Net.WebClient
        If Not DOCFileURL.Contains(".docx") Then
            If System.IO.File.Exists(workingfolder & "\PrintThisDOC.doc") Then System.IO.File.Delete(workingfolder & "\PrintThisDOC.doc")
            wc.DownloadFile(DOCFileURL, workingfolder & "\PrintThisDOC.doc")
            wc.Dispose()
            Return PrintThat(workingfolder & "\PrintThisDOC.doc", "", 60)
        Else
            If System.IO.File.Exists(workingfolder & "\PrintThisDOC.docx") Then System.IO.File.Delete(workingfolder & "\PrintThisDOC.docx")
            wc.DownloadFile(DOCFileURL, workingfolder & "\PrintThisDOC.docx")
            wc.Dispose()
            Return PrintThat(workingfolder & "\PrintThisDOC.docx", "", 60)
        End If
    End Function
End Module
